package titt.service.invoke;

import titt.head.HTTPHead;
import titt.head.request.MethodHead;
import titt.head.request.Request;
import titt.head.request.RequestHead;
import titt.io.IOFactory;
import titt.io.encoding.EncodingFacade;
import titt.type.Types;

public class UnknownMethodRedirect implements MethodParser {

	private final String methodName;
	private final MethodParser parser;
	private final EncodingFacade ef = IOFactory.createEncodingFacade();
	
	public UnknownMethodRedirect(String methodName, MethodParser parser) {
		this.methodName = methodName;
		this.parser = parser;
	}
	
	@Override
	public MethodHead parse(HTTPHead head) {
		
		RequestHead r = new Request(head);

		if(r.getPath().indexOf("?") == -1) {
			
			String m = methodName + "?string=" + ef.encodeToString(Types.STRING, (r.getPath().equals("/")? "/index.html" : r.getPath()));
			
			r = new Request(r.getHTTPMethod(), m, r);
		}

		return parser.parse(r);
	}

}
